export function timeHandle(timeVal, lan) {
  var publishTime = parseInt(timeVal / 1000);
  var d_seconds = '';
  var d_minutes = '';
  var d_hours = '';
  var d_days = '';
  var d_months = '';
  var d_years = '';
  var timeNow = parseInt(new Date().getTime() / 1000);
  var d = '';

  var date = new Date(publishTime * 1000);
  var Y = date.getFullYear();
  var M = date.getMonth() + 1;
  var D = date.getDate();
  var H = date.getHours();
  var m = date.getMinutes();
  var s = date.getSeconds();
  //小于10的在前面补0
  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }

  d = timeNow - publishTime;
  d_years = parseInt(d / 31536000);
  d_months = parseInt(d / 2592000);
  d_days = parseInt(d / 86400);
  d_hours = parseInt(d / 3600);
  d_minutes = parseInt(d / 60);
  d_seconds = parseInt(d);
  if (lan == 'km') {
    if (d_months >= 12) {
      return Y + '-' + M + '-' + D;
    } else if (d_days >= 30 && d_months <= 12) {
      return d_months + 'ខែមុន';
    } else if (d_days > 0 && d_days < 30) {
      return d_days + 'ថ្ងៃមុន';
    } else if (d_days <= 0 && d_hours > 0) {
      return d_hours + 'ម៉ោងមុន';
    } else if (d_hours <= 0 && d_minutes > 0) {
      return d_minutes + 'នាទីមុន';
    } else if (d_seconds < 60) {
      return 'អម្បាញ់មិញ';
    }
  } else if (lan == 'zh') {
    console.log('d_months', d_months);
    if (d_months >= 12) {
      return Y + '-' + M + '-' + D;
    } else if (d_days >= 30 && d_months <= 12) {
      return d_months + '月前';
    } else if (d_days > 0 && d_days < 30) {
      return d_days + '天前';
    } else if (d_days <= 0 && d_hours > 0) {
      return d_hours + '小时前';
    } else if (d_hours <= 0 && d_minutes > 0) {
      return d_minutes + '分钟前';
    } else if (d_seconds < 60) {
      return '刚刚';
    }
  } else if (lan == 'en') {
    if (d_months >= 12) {
      return Y + '-' + M + '-' + D;
    } else if (d_days >= 30 && d_months <= 12) {
      return d_months + 'Months ago';
    } else if (d_days > 0 && d_days < 30) {
      return d_days + 'days ago';
    } else if (d_days <= 0 && d_hours > 0) {
      return d_hours + 'hour ago';
    } else if (d_hours <= 0 && d_minutes > 0) {
      return d_minutes + 'minutes ago';
    } else if (d_seconds < 60) {
      return 'just now ';
    }
  }
}

//获取窗口可视范围的高度
export function getClientHeight() {
  var clientHeight = 0;
  if (document.body.clientHeight && document.documentElement.clientHeight) {
    var clientHeight =
      document.body.clientHeight < document.documentElement.clientHeight
        ? document.body.clientHeight
        : document.documentElement.clientHeight;
  } else {
    var clientHeight =
      document.body.clientHeight > document.documentElement.clientHeight
        ? document.body.clientHeight
        : document.documentElement.clientHeight;
  }
  return clientHeight;
}
//获取页面文档的总高度
export function documentHeight() {
  //现代浏览器（IE9+和其他浏览器）和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
  return Math.max(
    document.body.scrollHeight,
    document.documentElement.scrollHeight
  );
}

function getServerUserAgentLanguage(req) {
  return req.headers['accept-language'] || '';
}

function getClientUserAgentLanguage() {
  if (typeof window === 'undefined') return '';
  return window.navigator.language;
}

export function getUserAgentLanguage(...rest) {
  if (process.client) {
    return getClientUserAgentLanguage(...rest);
  }
  return getServerUserAgentLanguage(...rest);
}
